//
//  showViewController.m
//  FFmpeg_X264_Codec
//
//  Created by 陆永亨 on 16/1/8.
//  Copyright © 2016年 suntongmian@163.com. All rights reserved.
//

#import "showVideoListViewController.h"
#import "videoDB.h"
#import "showVideoViewController.h"
#import "KxMovieViewController.h"
@interface showVideoListViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property(strong,nonatomic)NSMutableArray * videoAry;
@end

@implementation showVideoListViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    videoDB * videodb = [[videoDB alloc]init];
    [[LosDatabaseHelper sharedInstance] inDatabase:^(FMDatabase *db) {
        
        _videoAry= [videodb selectAllVideowWithFMDBdatabase:db];
        [self.tableview reloadData];
    }];
    
    NSLogGreen(@"%ld",_videoAry.count);

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _tableview.tableFooterView = [UIView new];
    
    
  }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"showcelli"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"showcelli"];
    }
    
    cell.textLabel.text = _videoAry[indexPath.row];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _videoAry.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    showVideoViewController * showVideoVC = [[showVideoViewController alloc]init];
    showVideoVC.videoPath =_videoAry[indexPath.row];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:showVideoVC];
    
    [self presentViewController:nav animated:YES completion:nil];
  NSString * path=  _videoAry[indexPath.row];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!path) {
        return;
    }

    NSString *FilePath = [documentsDirectory stringByAppendingPathComponent:path];
    // increase buffering for .wmv, it solves problem with delaying audio frames
    if ([FilePath.pathExtension isEqualToString:@"wmv"])
        parameters[KxMovieParameterMinBufferedDuration] = @(5.0);
    
    // disable deinterlacing for iPhone, because it's complex operation can cause stuttering
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        parameters[KxMovieParameterDisableDeinterlacing] = @(YES);
    
    // disable buffering
    //parameters[KxMovieParameterMinBufferedDuration] = @(0.0f);
    //parameters[KxMovieParameterMaxBufferedDuration] = @(0.0f);
    
//    KxMovieViewController *vc = [KxMovieViewController movieViewControllerWithContentPath:FilePath
//                                                                               parameters:parameters];
//    [self presentViewController:vc animated:YES completion:nil];
    
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return @"删除";

}



-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath      //当在Cell上滑动时会调用此函数

{
    
    
    return  UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleNone;  //返回此值时,Cell会做出响应显示Delete按键,点击Delete后会调用下面的函数,别给传递UITableViewCellEditingStyleDelete参数
}



- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath //对选中的Cell根据editingStyle进行操作

{
  
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        videoDB * videodb = [[videoDB alloc]init];
        
        [[LosDatabaseHelper sharedInstance] inDatabase:^(FMDatabase *db) {
            
            [videodb deleteVideoWithFMDBdatabase:db path:_videoAry[indexPath.row]];
        }];
        
        NSFileManager* fileManager=[NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        
        //文件名
        NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:_videoAry[indexPath.row]];
        BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
        if (!blHave) {
            NSLog(@"no  have");
            return ;
        }else {
            NSLog(@" have");
            BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
            if (blDele) {
                NSLog(@"dele success");
            }else {
                NSLog(@"dele fail");
            }
            
        }
        
        [_videoAry removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
 
}


@end
